package com.newgoldcurrency.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.newgoldcurrency.R;

public class RainView extends View {
    private final Paint paint;
    private final Matrix matrix=new Matrix();
    private boolean isRun=false;
    private final FallBean fallBean=new FallBean();
    private int num=0;
    private ClickListener clickListener;

    private static class FallBean {
        public int offsetX,offsetY,x,y;
        public float scale;
        public Bitmap bitmap;
    }
    public RainView(Context context) {
        this(context, null);
    }
    public RainView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public RainView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
    }
    public void start(int i) {
        if (i<=0)return;
        this.num=i;
        this.isRun = true;
        fallBean.bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gift);
        fallBean.x = 500;
        fallBean.offsetX = getRandom();
        fallBean.offsetY=3;
        fallBean.scale=0.3f;
        fallBean.y=0;
        postInvalidate();
    }
    private int getRandom(){
        return (int)(-1 +Math.random()* 1);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isRun) {
            boolean isInScreen = false;
            matrix.reset();
            matrix.setScale(fallBean.scale, fallBean.scale);
            fallBean.x = fallBean.x + fallBean.offsetX;
            fallBean.y = fallBean.y + fallBean.offsetY;
            if (fallBean.y <= getHeight()) isInScreen = true;
            matrix.postTranslate(fallBean.x, fallBean.y);
            canvas.drawBitmap(fallBean.bitmap, matrix, paint);
            if (isInScreen) {
                postInvalidate();
            } else {
                release();
            }
        }
    }
    private void release() {
        if(fallBean.bitmap!=null) if(!fallBean.bitmap.isRecycled()) fallBean.bitmap.recycle();
        num--;
        if (num>0)start(num);
    }
    public void addClickListener(ClickListener clickListener){
        this.clickListener=clickListener;
    }
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN&&isRun) {
            float downX = event.getX();
            float downY = event.getY();
            double w = getWidth() * 0.3;
            if (downX>fallBean.x&&downX<fallBean.x+w){
                if (downY>fallBean.y&&downY<fallBean.y+w){
                    if (clickListener!=null) {
                        this.num=0;
                        this.isRun = false;
                        fallBean.bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gift);
                        fallBean.x = 0;
                        fallBean.offsetX = 0;
                        fallBean.offsetY=0;
                        fallBean.scale=0;
                        fallBean.y=0;
                        postInvalidate();
                        clickListener.click();
                        return true;
                    }
                }
            }
        }
        return false;
    }
    public interface ClickListener{
        void click();
    }
}